<?php
use think\cache\driver\Redis;
// 应用公共文件
function getSnowId($dataCenterId = 0, $machineId = 0){
    $snow = new Snow\Snow();
    return $snow->createOnlyId($dataCenterId,$machineId);
}
if(!function_exists("check_request")){
    function check_request($arr=[],$key=null){
        if(isset($arr[$key])&&!empty($arr[$key])){
            return true;
        }else{
            return false;
        }
    }
}
if(!function_exists("user_balance_log_orderType")){
    function user_balance_log_orderType($val,$return_all=false){
        $order_type=array(
            1=>"提现",
            2=>"分享佣金",
            3=>"作家版权费",
            4=>"NFT卖出订单",
            5=>'提现失败',
        );
        if($return_all==true){
            return $order_type;
        }
        if(isset($order_type[$val])){
            return $order_type[$val];
        }else{
            return "未知";
        }
    }
}
if(!function_exists("platform_money_log_capitalType")){
    function platform_money_log_capitalType($val,$return_all=false){
        $order_type=array(
            1=>"平台服务费",
            2=>"注册钱包地址",
            3=>"生成作品码",
            4=>"转移作品码",
            5=>"影印品订单",
        );
        if($return_all==true){
            return $order_type;
        }
        if(isset($order_type[$val])){
            return $order_type[$val];
        }else{
            return "未知";
        }
    }
}

/*
*
*返回字符串的毫秒数时间戳
*/
if(!function_exists("get_total_millisecond")) {
    function get_total_millisecond()
    {
        list($msec, $sec) = explode(' ', microtime());
        $msectime =  (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
        $exp=explode('.',$msectime);
        return $exp[0];
    }
}

/*
*
*返回用户年龄
*/
if(!function_exists("get_user_age")) {
    function get_user_age($card_id)
    {
        if(!empty($card_id)){
            # 1.从身份证中获取出生日期
            $birth_Date = strtotime(substr($card_id, 6, 8));//截取日期并转为时间戳

            # 2.格式化[出生日期]
            $Year = date('Y', $birth_Date);//yyyy
            $Month = date('m', $birth_Date);//mm
            $Day = date('d', $birth_Date);//dd

            # 3.格式化[当前日期]
            $current_Y = date('Y');//yyyy
            $current_M = date('m');//mm
            $current_D = date('d');//dd

            # 4.计算年龄()
            $age = $current_Y - $Year;//今年减去生日年
            if($Month > $current_M || $Month == $current_M && $Day > $current_D){//深层判断(日)
                $age--;//如果出生月大于当前月或出生月等于当前月但出生日大于当前日则减一岁
            }
            if($age<18){
                return ['code'=>-1,'msg'=>'平台禁止未成年交易'];
            }
            return ['code'=>1,'msg'=>'success'];
        }else{
            return ['code'=>-1,'msg'=>'请绑定身份证'];
        }
    }
}


//设置锁定作品编码
function setOpusNumberLock($opus_number_id=[]){
    $redis=new Redis();
    $redis->set('lockOrderOpusOpusNumber'.$opus_number_id,1,720);
}
//设置锁定作品编码
function delOpusNumberLock($opus_number_id){
    $redis=new Redis();
    $redis->del('lockOrderOpusOpusNumber'.$opus_number_id);
}
//查询作品编号是否被锁定
function checkOpusNumberLock($id){
    $redis=new Redis();
    return $redis->get('lockOrderOpusOpusNumber'.$id);
}
function checkOpusNumberLockReturn($id){
    $res=checkOpusNumberLock($id);
    if($res){
        show_json('wxapp.opusNumber.lock');
    }
}
/*
*
*雪花id
*/
if(!function_exists("getSnow")) {
    function getSnow($dataCenterId = 0, $machineId = 0)
    {
        return (new \Snow\Snow())->createOnlyId($dataCenterId, $machineId);
    }
}

if(!function_exists("GetRandStr")) {
    function GetRandStr($length)
    {
        $str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        $len = strlen($str) - 1;
        $randstr = '';
        for ($i = 0; $i < $length; $i++) {
            $num = mt_rand(0, $len);
            $randstr .= $str[$num];
        }
        return $randstr;
    }
}
if(!function_exists("hidestr")) {
    function hidestr($string, $start = 0, $length = 0, $re = '*')
    {
        if (empty($string)) return false;
        $strarr = array();
        $mb_strlen = mb_strlen($string);
        while ($mb_strlen) {
            $strarr[] = mb_substr($string, 0, 1, 'utf8');
            $string = mb_substr($string, 1, $mb_strlen, 'utf8');
            $mb_strlen = mb_strlen($string);
        }
        $strlen = count($strarr);
        $begin = $start >= 0 ? $start : ($strlen - abs($start));
        $end = $last = $strlen - 1;
        if ($length > 0) {
            $end = $begin + $length - 1;
        } elseif ($length < 0) {
            $end -= abs($length);
        }
        for ($i = $begin; $i <= $end; $i++) {
            $strarr[$i] = $re;
        }
        if ($begin >= $end || $begin >= $last || $end > $last) return false;
        return implode('', $strarr);
    }
}
/**
 * 只保留字符串首尾字符，隐藏中间用*代替（两个字符时只显示第一个）
 * @param string $user_name 姓名
 * @param string $repeatStr 替换的字符
 * @param string $encode 字符编码
 * @return string 格式化后的姓名
 */
if(!function_exists("userNameCut")){
    function userNameCut($user_name, $repeatStr = '*', $encode = 'utf-8'){
        if (empty($user_name)) {
            return '***';
        }
        $length = mb_strlen($user_name, $encode);
        $firstStr = mb_substr($user_name, 0, 1, $encode);
        $lastStr = mb_substr($user_name, -1, 1, $encode);
        return $length == 2 ? $firstStr . str_repeat($repeatStr, $length - 1) : $firstStr . str_repeat($repeatStr, $length - 2) . $lastStr;
    }
}